<?php

/**
 * 评价监控
 */

namespace App\Http\Controllers\Assess;

use App\Http\Controllers\Backend;
use App\Imports\AmazonCommentImport;
use App\Imports\OrderImport;
use App\Services\Assess\EvaluationMonitoringService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class EvaluationMonitoringController extends Backend
{

    /**
     * 构造函数
     * @author wuyu
     * @since 2023/4/21
     * LinkController constructor.
     */
    public function __construct()
    {
        parent::__construct();
        $this->service = new EvaluationMonitoringService();
    }

    public function getEvaluationMonitoringConfig()
    {
        return $this->service->getEvaluationMonitoringConfig();
    }

    public function detail()
    {
        return $this->service->detail();
    }

    public function importAsinComment(Request $request)
    {

        try {
            $result = upload_file($request);
            if (!$result['success']) {
                return message($result['msg'], false);
            }
            // 文件路径
            $file_path = $result['data']['file_path'];
            if (!$file_path) {
                return message("文件上传失败", false);
            }
            $fileOriginalName = $result['data']['file_original_name'] ?? '';
            $fileOriginalNameExp = explode('采集',$fileOriginalName);
            if (count($fileOriginalNameExp)<=1) return message("导入excel格式不正确", false);

            // 文件绝对路径
            $file_path = ATTACHMENT_PATH . $file_path;
            $AmazonCommentImport = new AmazonCommentImport();
            $AmazonCommentImport->asin = $fileOriginalNameExp[0] ?? '';
            // 导入Excel
            Excel::import($AmazonCommentImport, $file_path);
            unlink($file_path);
            if (!empty($AmazonCommentImport->message)) {
                throw new \Exception(implode('<br/>', $AmazonCommentImport->message));
            }
        } catch (\Exception $e) {
            return message("导入信息:<br/>" . $e->getMessage(), false);
        }
        return message("导入成功", true);
    }


    public function getGiftConfig()
    {
        return $this->service->getGiftConfig();
    }

}


